function saveOption(frmID, resp , callback)
{
	try
	{
		$(frmID).request(
			{
				asynchronous: true,
				parameters: $(frmID).serialize(true),
		  		onComplete: function(transport)
		  		{ 
				  	var response = transport.responseText || "";
			    	
			    	$(resp).innerHTML = '';
			    	//alert(response);
			    	if(response != "")
			    	{
			    		obj = response.evalJSON();
			    		
			    		if(obj == null || obj.message == '')
			    		{
			    			$(resp).innerHTML = 'Something wrong! Reponsed string is: ' + response;
			    		}
			    		
				    	if(resp != null)
					    	$(resp).innerHTML = obj.message;
					    	
					    if(obj.code == '0' && callback != null)
					    {
					    	callback(obj);
					    }
			    	}
		  		}
			});
	
		$(resp).show();
		$(resp).style.display = 'inherit';
		Fat.fade_element(resp, null, 1500, null, null);
	}
	catch(exp)
	{
		alert('Exception in option script');
	}

	//revent submit page
	return false;
}

function toggle_panel(country_id)
{
	if(country_id != -1)
	{
		$('new_panel').hide();
		$('btn_add_panel').hide();
		$('btn_update_panel').show();
	}
	else
	{
		$('new_panel').show();
		$('btn_add_panel').show();
		$('btn_update_panel').hide();
	}
	
	$('country_code').value = "";
	$('country_name').value = "";
	
	$('lang_place_holder').childElements().each(
					function(o) { 
						if(o.id.startsWith('chk'))
							o.checked = '';		
					});
}

function onChangeCountry(country_id)
{
	toggle_panel(country_id);
	
	new Ajax.Request('?mod=options', 
	{
	  method: 'post',
	  asynchronous: true,
	  parameters:'act=request_lang&id=' + country_id,
	  onSuccess: function(transport) 
  		{
			var response = transport.responseText;
			$('lang_place_holder').update(response);
			
			$('update_lang_response').hide();
		}
	});
}

function deleteCountry(objSelectBox)
{
	$('act').value='deletecountry';
	
	if(!confirm('Are you sure you want to delete selected country?'))
	{
		return false;
	}
	
	var callback = function(resObj) 
					{ 
						if(resObj != null)
						{
							//alert(objSelectBox.options.length);
							for (var i=(objSelectBox.options.length-1); i>0; i--)	//i >0: first item is new, we dont't remove
							{
								var o = objSelectBox.options[i];
								if (o.selected)
								{
									objSelectBox.remove(i);
									toggle_panel(-1);
									break;
								}
							}
						}
					}
	
	saveOption('option_country_lang', 'update_lang_response', callback);
}

function addCountry()
{
	$('act').value = 'addcountry';
	$('country_code').value = $('country_code').value.trim();
	$('country_name').value = $('country_name').value.trim();
	
	if($('country_code').value == '')
	{
		alert('Please input country code.');
		return false;
	}
	
	if($('country_name').value == '')
	{
		alert('Please input country name.');
		return false;
	}
	
	var callback = function(resObj) 
					{ 
						toggle_panel(-1);
						
						if(resObj != null)
						{
							$('country').innerHTML += '<option value="' + obj.country_code  + '">' + obj.country_name + '</option>';
						}
					}
	
	saveOption('option_country_lang', 'update_lang_response', callback);
}